{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据：\n",
      "[2, 5, 3, 1, 10, 4]\n",
      "大根堆排序：\n",
      "[10, 5, 4, 3, 2, 1]\n",
      "原始数据：\n",
      "[345, 312, 65, 765, 143, 564]\n",
      "小根堆排序：\n",
      "[65, 143, 312, 345, 564, 765]\n"
     ]
    }
   ],
   "source": [
    "# coding:utf-8\n",
    "# 交换数据元素\n",
    "def swap(tree, i, j):\n",
    "    temp = tree[i]\n",
    "    tree[i] = tree[j]\n",
    "    tree[j] = temp\n",
    " \n",
    " \n",
    "# 调整堆，调整为大根堆\n",
    "def heapify(tree, i):\n",
    "    if i >= len(tree):\n",
    "        return\n",
    "    c1 = 2 * i + 1\n",
    "    c2 = 2 * i + 2\n",
    "    max = i\n",
    "    if c1 < len(tree) and tree[c1] > tree[max]:\n",
    "        max = c1\n",
    "    if c2 < len(tree) and tree[c2] > tree[max]:\n",
    "        max = c2\n",
    "    if max != i:\n",
    "        swap(tree, max, i)\n",
    "        heapify(tree, max)\n",
    " \n",
    " \n",
    "# 构造堆\n",
    "def build_heap(tree):\n",
    "    last_node = len(tree) - 1\n",
    "    parent = int((last_node - 1) / 2)\n",
    "    for i in range(parent, -1, -1):\n",
    "        heapify(tree, i)\n",
    " \n",
    " \n",
    "# 堆排序.大根堆，降序\n",
    "def heap_sort(tree):\n",
    "    build_heap(tree)\n",
    "    result = []\n",
    "    for i in range(len(tree) - 1, -1, -1):\n",
    "        # 交换根节点与最后一个节点,砍断最后一个节点，重新调整堆\n",
    "        swap(tree, i, 0)\n",
    "        result.append(tree.pop())\n",
    "        heapify(tree, 0)\n",
    "    return result\n",
    " \n",
    " \n",
    "# 调整堆，调整为小根堆\n",
    "def heapify1(tree, i):\n",
    "    if i >= len(tree):\n",
    "        return\n",
    "    c1 = 2 * i + 1\n",
    "    c2 = 2 * i + 2\n",
    "    min = i\n",
    "    if c1 < len(tree) and tree[c1] < tree[min]:\n",
    "        min = c1\n",
    "    if c2 < len(tree) and tree[c2] < tree[min]:\n",
    "        min = c2\n",
    "    if min != i:\n",
    "        swap(tree, min, i)\n",
    "        heapify(tree, min)\n",
    " \n",
    " \n",
    "# 构造小根堆\n",
    "def build_heap1(tree):\n",
    "    last_node = len(tree) - 1\n",
    "    parent = int((last_node - 1) / 2)\n",
    "    for i in range(parent, -1, -1):\n",
    "        heapify1(tree, i)\n",
    " \n",
    " \n",
    "# 堆排序.小根堆，升序\n",
    "def heap_sort1(tree):\n",
    "    build_heap1(tree)\n",
    "    result = []\n",
    "    for i in range(len(tree) - 1, -1, -1):\n",
    "        # 交换根节点与最后一个节点,砍断最后一个节点，重新调整堆\n",
    "        swap(tree, i, 0)\n",
    "        result.append(tree.pop())\n",
    "        heapify1(tree, 0)\n",
    "    return result\n",
    " \n",
    " \n",
    "if __name__ == \"__main__\":\n",
    "    tree = [2, 5, 3, 1, 10, 4]\n",
    "    print(\"原始数据：\")\n",
    "    print(tree)\n",
    "    result = heap_sort(tree)\n",
    "    print(\"大根堆排序：\")\n",
    "    print(result)\n",
    " \n",
    "    # 小根堆测试用例\n",
    "    tree = [345, 312, 65, 765, 143, 564]\n",
    "    print(\"原始数据：\")\n",
    "    print(tree)\n",
    "    result = heap_sort1(tree)\n",
    "    print(\"小根堆排序：\")\n",
    "    print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:zhong]",
   "language": "python",
   "name": "conda-env-zhong-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
